package com.junjie.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.junjie.dao.IUserDao;
import com.junjie.entity.User;
import com.junjie.util.ConfigUtil;
import com.junjie.util.DBUtil;

public class UserDaoImpl implements IUserDao{
	/**
	 * 更新用户信息,这是一个统一的方法,用户要想按需要更新信息 只要调用此方法就行
	 */
	public int update(String sql, Object[] parameter) throws Exception {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int id = 0;
	try {
		conn = DBUtil.getConnection();
		pstmt = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
		if(parameter != null && parameter.length>0){
			for (int i = 1; i <= parameter.length; i++) {
				pstmt.setObject(i, parameter[i-1]);
			}
		}
		int result = pstmt.executeUpdate();
		if(result>0){
			rs = pstmt.getGeneratedKeys();
			if(rs.next())
			id = rs.getInt(1);
		}
	} catch (Exception e) {
		e.printStackTrace();
	}finally{
		DBUtil.close(rs, pstmt, conn);
	}
	return id;
	}
	/**
	 * 实现接口,用来统一实现对数据要各种要求进行查询
	 * 返回List<User>集合,用户根据需要在定义具体的方法
	 */
	public List<User> select(String sql, Object[] parameter) throws Exception {
			List<User> userList = new ArrayList<User>();
			Connection conn = null;
			PreparedStatement pstmt = null;
			ResultSet rs = null;
		try {
			conn = DBUtil.getConnection();
			pstmt = conn.prepareStatement(sql);
			if(parameter != null && parameter.length>0){
				for (int i = 1; i <= parameter.length; i++) {
					pstmt.setObject(i, parameter[i-1]);
				}
			}
			rs = pstmt.executeQuery();
			while(rs.next()){
				User user = new User();
				user.setId(rs.getInt("id"));
				user.setEmail(rs.getString("email"));
				user.setNickName(rs.getString("nickname"));
				user.setPassword(rs.getString("password"));
				user.setUserIntegral(rs.getInt("user_integral"));
				user.setEmailVerify((rs.getString("is_email_verify").trim().equals("Y"))?true:false);
				user.setEmailVerifyCode(rs.getString("email_verify_code"));
				user.setLastLoginTime(rs.getLong("last_login_time"));
				user.setLastLoginIp(rs.getString("last_login_ip"));
				userList.add(user);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtil.close(rs, pstmt, conn);
		}
		return userList;
	}
	/**
	 * 添加用户,调用update()方法 
	 * @return id 用户的id
	 * @param args
	 * @throws Exception 
	 */
	public int addUser(User user) throws Exception{
		return update(ConfigUtil.getValue("ADD_USER"),new Object[]{user.getEmail(),user.getNickName(),user.getPassword(),user.isEmailVerify()?"Y":"N",user.getEmailVerifyCode()});
	}
	/**
	 * 根据ID 查询用户
	 * @throws Exception 
	 */
	public User findById(int id) throws Exception{
		return select(ConfigUtil.getValue("FIND_BY_ID"), new Object[]{id}).get(0);
	}
	/**
	 * 根据邮箱验证码查询用户返回用户对象
	 * @param args
	 * @throws Exception 
	 */
	public User findByEmailCode(String emailCode) throws Exception{
		List<User> userList = select(ConfigUtil.getValue("FIND_BY_EMAILCODE"), new Object[]{emailCode});
		if(userList.size()>0){
			return userList.get(0);
		}else{
			return null;
		}
	}
	/**
	 * 根据邮箱地址查询邮箱是否已经被注册
	 * @param args
	 * @throws Exception 
	 */
	public int findByEmailAdd(String emailAddress) throws Exception{
	return select(ConfigUtil.getValue("FIND_BY_EMAILADD"), new Object[]{emailAddress}).size();
		
	}
	/**
	 * 根据Id更新用户信息设置is_mail_verify = Y
	 * @param args
	 */
	public void changVerify(int id)throws Exception{
		update(ConfigUtil.getValue("CHANGE_EMAIL_VERIFY"),new Object[]{id});
		User u = findById(id);
		u.setEmailVerify(true);
	}
	/**
	 * 登录验证:根据用户输入的邮箱和密码验证用户是否存在
	 * @param args
	 */
	public User login(String email,String pwd)throws Exception{
		List<User> userList = select(ConfigUtil.getValue("FIND_BY_EMAIL_PWD"),new Object[]{email,pwd});
		if(userList.size()>0){
			return userList.get(0);
		}else{
			return null;
		}
	}
	/**
	 * 用户登录成功后修改数据库中的登录时间 和IP地址
	 * @param args
	 * @throws Exception 
	 */
	public void updateTimeIp(long time,String ip,int id) throws Exception{
		update(ConfigUtil.getValue("UPDATE_TIME_IP"),new Object[]{time,ip,id});
	}
	public static void main(String[] args) {
		User user = new User();
		user.setEmail("junjie2@126.com");
		user.setNickName("junjie");
		user.setPassword("123");
		
		try {
			System.out.println(new UserDaoImpl().addUser(user));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
}
